﻿using MyNet.Library.Core;
using MyNet.Library.Mvc;
using MyNet.Library.Shared;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Web;
using System.Web.Mvc;

namespace Coffee.Web
{
    public class HomeController : BaseController
    {
        // GET: Home
        public ActionResult Index()
        {
            return View();
        }

        public ActionResult GetTouTiaoList(ActionOptions actionOptions)
        {
            actionOptions.ResultType = ActionResultType.Json;

            var url = "https://www.toutiao.com/pgc/ma/?page_type=1&max_behot_time=1523850813&uid=4369928976&media_id=4369928976&output=json&is_json=1&count=20&from=user_profile_app&version=2&as=A1458B10F56606A&cp=5B058680D69A5E1&callback=";


            var result = SendRequest(url);

            return Result(actionOptions, SerializationHelper.JsonDeserialize(result));
        }

        public ActionResult Logout(ActionOptions actionOptions)
        {
            actionOptions.ResultType = ActionResultType.Json;
            ManagerAuthorization.Current.SignOut();
            return Result(actionOptions);
        }

        [Power(false)]
        public ActionResult MNLogin(ActionOptions actionOptions, int id)
        {
            actionOptions.ResultType = ActionResultType.Json;

            var ticket = ManagerAuthorization.Current.SetAuthTicket(id, "");

            return Result(actionOptions, new { Ticket = ticket });
        }

        public ActionResult GetTouTiaoInfo(ActionOptions actionOptions)
        {
            actionOptions.ResultType = ActionResultType.Json;

            var url = "https://m.toutiao.com/i6555715558698910215/info/";


            var result = SendRequest(url);

            return Result(actionOptions, SerializationHelper.JsonDeserialize(result));
        }


        /// <summary>  
        /// 指定Post地址使用Get 方式获取全部字符串  
        /// </summary>  
        /// <param name="url">请求后台地址</param>  
        /// <returns></returns>  
        public static string Post(string url, string content = "")
        {
            string result = "";
            HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
            req.Method = "POST";
            req.ContentType = "application/x-www-form-urlencoded";

            #region 添加Post 参数  
            byte[] data = Encoding.UTF8.GetBytes(content);
            req.ContentLength = data.Length;
            using (Stream reqStream = req.GetRequestStream())
            {
                reqStream.Write(data, 0, data.Length);
                reqStream.Close();
            }
            #endregion

            HttpWebResponse resp = (HttpWebResponse)req.GetResponse();
            Stream stream = resp.GetResponseStream();
            //获取响应内容  
            using (StreamReader reader = new StreamReader(stream, Encoding.UTF8))
            {
                result = reader.ReadToEnd();
            }
            return result;
        }

        public static String SendRequest(String url)
        {
            HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(url);
            webRequest.Method = "GET";
            HttpWebResponse webResponse = (HttpWebResponse)webRequest.GetResponse();
            StreamReader sr = new StreamReader(webResponse.GetResponseStream(), Encoding.UTF8);
            return sr.ReadToEnd();
        }

    }
}